<?php
/**
 * 后台时间管理
 *
 * 2017-7-10
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class TimeController extends AdminbaseController
{
    protected $time_model;

    public function _initialize() {
        parent::_initialize();
        $this->time_model = D("Common/Time");
    }
    //列表
    public function index(){
        $list = $this->time_model->where('status = 1')->select();$list = $this->time_model->where('status = 1')->select();
        foreach ($list as &$item) {
            $time_solt =  explode('-',$item['time_solt']);
            unset($item['time_solt']);
            list($item['start_time'],$item['end_time']) = $time_solt;
            switch ($item['type']) {
                case 1:$item['type'] = '常规';break;
                case 2:$item['type'] = '其他';break;
            }
        }
        $this->assign('list',$list);
        $this->display();
    }
    //添加
    public function add()
    {
        if (IS_POST) {
            $param = I("post.");
            if ($param['start_time'] == '' || $param['end_time'] == '') {
                $this->error("请选择时间段");
            }
            $data = array('time_solt'=>$param['start_time'].'-'.$param['end_time'],'type'=>$param['type']);
            if ($this->time_model->create($data) !== false) {
                $result=$this->time_model->add($data);
                if ($result !== false) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->users_model->getError());
            }
        } else {
            $this->assign('auction','add');
            $this->display();
        }
    }

    //编辑
    public function edit()
    {
        if (IS_POST) {
            $param = I("post.");
            if ($param['start_time'] == '' || $param['end_time'] == '') {
                $this->error("请选择时间段");
            }
            $data = array('id'=>$param['id'],'time_solt'=>$param['start_time'].'-'.$param['end_time'],'type'=>$param['type']);
            if ($this->time_model->create($data) !== false) {
                $result=$this->time_model->save($data);
                if ($result !== false) {
                    $this->success('保存成功');
                } else {
                    $this->error('修改失败');
                }
            } else {
                $this->error($this->users_model->getError());
            }

        } else {
            $id = I("get.id",0,'intval');
            $list = $this->time_model->where('id = %d',$id)->find();
            if ($list != false) {
                $time_solt =  explode('-',$list['time_solt']);
                unset($list['time_solt']);
                list($list['start_time'],$list['end_time']) = $time_solt;
            }
            $this->assign('list',$list);
            $this->display();
        }
    }

    //删除
    public function del()
    {
        $id = I("get.id",0,'intval');
        if($this->time_model->delete($id) !== false) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}